package eu.dominicum.ft.ui.web.jsf.view.bean;

import eu.dominicum.ft.pd.boundary.FantasyTourManagement;
import org.apache.myfaces.trinidad.context.RequestContext;

import javax.ejb.EJBException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.inject.Inject;

/**
 * Backing bean for the calculate-scores-for-stage-dialog view.
 *
 * @author Harry Klerks
 * @since 2013M1
 */
@ManagedBean(name = "calculateScoresForStageDialog")
@ViewScoped
public final class CalculateScoresForStageDialogBean {

    /**
     * {@link eu.dominicum.ft.pd.boundary.FantasyTourManagement}.
     */
    @Inject
    private FantasyTourManagement fantasyTourManagement;

    /**
     * Event handler that fires when the Start button is clicked.
     *
     * @param actionEvent -
     */
    public void calculateScoresForStage(final ActionEvent actionEvent) {

        try {
            Long fantasyStageId = Long.parseLong((String) RequestContext.getCurrentInstance().getPageFlowScope().get("fantasyStageId"));
            this.fantasyTourManagement.calculateScoresForStage(fantasyStageId);
        } catch (EJBException eex) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    eex.getMessage(), null));

        }

    }
}
